#!/bin/sh

# Munin plugin to measure ONKI Light response time

DATAPATH=/data/onki-light-tests/performance
#DATAPATH=.
# TODO DATAPATH should be set with munin environment variables
URLFILE=$DATAPATH/onki-dynamic-urls-page.txt
RESPONSELOG=$DATAPATH/responsetime.log
TIME=20S

# categories of web response time benchmark
CATS=`grep ' D' $RESPONSELOG |awk '{ print $2 }'|cut -c 2-|sort|uniq`

if [ "$1" = "config" ]; then
  echo 'graph_title ONKI web page response time'
  echo 'graph_vlabel seconds'
  echo 'graph_category onki'
  
  echo 'current_page.label current page'
  
  # other categories determined from log file
  for cat in $CATS; do
    echo "$cat.label $cat"
  done
  
  exit 0
fi

# print values from last run
for cat in $CATS; do
  echo -n "$cat.value "
  grep -A 1 " D$cat" $RESPONSELOG |tail -n 1|cut -d ',' -f 5|tr -d ' '
done

# determine current value for web page requests
echo -n "current_page.value "
/usr/bin/siege --rc=/dev/null -c 1 -H 'Pragma: no-cache' --log=/dev/null -t $TIME -i -f $URLFILE \
  2>&1 >/dev/null| grep 'Response time'|awk '{ print $3 }'
